//
//  ViewController.m
//  sdkdemo-iOS
//
//  Created by 范保莹 on 2018/6/2.
//  Copyright © 2018年 sdkdemo-iOS. All rights reserved.
//

#import "ViewController.h"

#import "BluetoothViewController.h"
#import "PinPadViewController.h"
#import "EMVViewController.h"
#import "CardReaderViewController.h"
#import "DisplayViewController.h"
#import "TerminalViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(strong, nonatomic)UIView *headerView;

@property(strong, nonatomic)UIView *menuView;

@property(strong, nonatomic)UIView *contentView;

@property(strong,nonatomic)UITableView *myTableView;

@property(strong,nonatomic)NSArray *dataArr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.dataArr = @[@"蓝牙模块",@"PINPAD模块",@"EMV模块",@"CARDREADER模块",@"DISPLAY模块",@"TERMINAL模块"];
    
    [self header];
    
    UILabel *lab1 = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/6, 0, 3, SCREEN_HEIGHT)];
    lab1.backgroundColor = bgColor;
    [self.view addSubview:lab1];
    
    UILabel *lab2 = [[UILabel alloc]initWithFrame:CGRectMake(0, SCREEN_WIDTH/6+70, SCREEN_WIDTH/6, 3)];
    lab2.backgroundColor = bgColor;
    [self.view addSubview:lab2];
    
    [self menu];
    
    [self content];
    
    CardReaderViewController *bvc = [[CardReaderViewController alloc]init];
    bvc.titleStr = _dataArr[0];
    bvc.view.frame = CGRectMake(0, 0, SCREEN_WIDTH*5/6, SCREEN_HEIGHT);
    [self addChildViewController:bvc];
    [self.contentView addSubview:bvc.view];

}

- (void)header {
    
    self.headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH/6, SCREEN_WIDTH/6+50)];
    self.headerView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:_headerView];
    
    UIImageView *headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH/6-20, SCREEN_WIDTH/6-20)];
    headerImageView.image = [UIImage imageNamed:@"icon"];
    [self.headerView addSubview:headerImageView];
    
    UILabel *nameLab = [[UILabel alloc]initWithFrame:CGRectMake(0, SCREEN_WIDTH/6, SCREEN_WIDTH/6, 20)];
    nameLab.text = @"SDKDemo";
    nameLab.font = [UIFont systemFontOfSize:12.0];
    nameLab.textAlignment = NSTextAlignmentCenter;
    [self.headerView addSubview:nameLab];
    
    UILabel *logLab = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/12, SCREEN_WIDTH/6+20, SCREEN_WIDTH/12, 20)];
    logLab.text = @"1.0.2";
    logLab.font = [UIFont systemFontOfSize:12.0];
    logLab.textAlignment = NSTextAlignmentCenter;
    logLab.backgroundColor = bgColor;
    [self.headerView addSubview:logLab];
}

- (void)menu {
    
    self.menuView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_WIDTH/6+73, SCREEN_WIDTH/6, SCREEN_HEIGHT-SCREEN_WIDTH/6-73)];
    [self.view addSubview:_menuView];
    
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH/6, SCREEN_HEIGHT-SCREEN_WIDTH/6-73)];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [_myTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
    [self tableView:_myTableView didSelectRowAtIndexPath:indexPath];
    [self.menuView addSubview:_myTableView];
    
}

- (void)content {
    
    self.contentView = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/6+3, 20, SCREEN_WIDTH*5/6, SCREEN_HEIGHT)];
    [self.view addSubview:_contentView];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 60;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selectedBackgroundView = [[UIView alloc]initWithFrame:cell.bounds];
    cell.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:105/255.0 green:150/255.0 blue:200/255.0 alpha:1];
    
    [self.contentView removeFromSuperview];
    
    [self content];
    
    if (indexPath.row == 0) {
        BluetoothViewController *bvc = [[BluetoothViewController alloc]init];
        bvc.titleStr = _dataArr[indexPath.row];
        bvc.view.frame = CGRectMake(0, 0, SCREEN_WIDTH*5/6, SCREEN_HEIGHT);
        [self addChildViewController:bvc];
        [self.contentView addSubview:bvc.view];
    }else if (indexPath.row == 1){
        
        PinPadViewController *bvc = [[PinPadViewController alloc]init];
        bvc.titleStr = _dataArr[indexPath.row];
        bvc.view.frame = CGRectMake(0, 0, SCREEN_WIDTH*5/6, SCREEN_HEIGHT);
        [self addChildViewController:bvc];
        [self.contentView addSubview:bvc.view];
    }else if (indexPath.row == 2){
        
        EMVViewController *bvc = [[EMVViewController alloc]init];
        bvc.titleStr = _dataArr[indexPath.row];
        bvc.view.frame = CGRectMake(0, 0, SCREEN_WIDTH*5/6, SCREEN_HEIGHT);
        [self addChildViewController:bvc];
        [self.contentView addSubview:bvc.view];
    }else if (indexPath.row == 3){
        
        CardReaderViewController *bvc = [[CardReaderViewController alloc]init];
        bvc.titleStr = _dataArr[indexPath.row];
        bvc.view.frame = CGRectMake(0, 0, SCREEN_WIDTH*5/6, SCREEN_HEIGHT);
        [self addChildViewController:bvc];
        [self.contentView addSubview:bvc.view];
    }else if (indexPath.row == 4){
        
        DisplayViewController *bvc = [[DisplayViewController alloc]init];
        bvc.titleStr = _dataArr[indexPath.row];
        bvc.view.frame = CGRectMake(0, 0, SCREEN_WIDTH*5/6, SCREEN_HEIGHT);
        [self addChildViewController:bvc];
        [self.contentView addSubview:bvc.view];
    }else if (indexPath.row == 5){
        
        TerminalViewController *bvc = [[TerminalViewController alloc]init];
        bvc.titleStr = _dataArr[indexPath.row];
        bvc.view.frame = CGRectMake(0, 0, SCREEN_WIDTH*5/6, SCREEN_HEIGHT);
        [self addChildViewController:bvc];
        [self.contentView addSubview:bvc.view];
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    cell.textLabel.text = _dataArr[indexPath.row];
    cell.textLabel.textColor = bgColor;
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.font = [UIFont systemFontOfSize:14.0];
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
